package naga.x.platform2.channel.weixin.support;

import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.annotation.JSONField;

import naga.x.platform2.utils.CommonUtils;

/**
 * 返回给客户端的订单信息
 * @author YangLi
 *
 */
public class OrderResponse {

	protected int code;
	protected PayConfig payconfig;
	
	public static class PayConfig {
//		 appId: result.appid,
		protected String appId;
//         timeStamp: Date.now() / 1000,
		protected String timeStamp;
//         nonceStr: Utils.randomString(),
		protected String nonceStr;
//         package: 'prepay_id=' + result.prepay_id,
		@JSONField(name = "package")
		protected String _package;
//         signType: 'MD5'
		protected String signType;
		protected String paySign;
		public PayConfig(GetOrderResponse res) {
			this.appId = res.getAppid();
			this.timeStamp = String.valueOf(System.currentTimeMillis() / 1000);
			this.nonceStr = CommonUtils.randomText(32);
			this._package = "prepay_id=" + res.getPrepay_id();
			this.signType = "MD5";
			
			Map<String, String> map = new TreeMap<String, String>();
			map.put("appId", appId);
			map.put("timeStamp", timeStamp);
			map.put("nonceStr", nonceStr);
			map.put("package", _package);
			map.put("signType", signType);
			
			this.paySign = WeiXinApi.makeSign(map);
		}
		public String getAppId() {
			return appId;
		}
		public void setAppId(String appId) {
			this.appId = appId;
		}
		public String getTimeStamp() {
			return timeStamp;
		}
		public void setTimeStamp(String timeStamp) {
			this.timeStamp = timeStamp;
		}
		public String getNonceStr() {
			return nonceStr;
		}
		public void setNonceStr(String nonceStr) {
			this.nonceStr = nonceStr;
		}
		public String get_package() {
			return _package;
		}
		public void set_package(String _package) {
			this._package = _package;
		}
		public String getSignType() {
			return signType;
		}
		public void setSignType(String signType) {
			this.signType = signType;
		}
		public String getPaySign() {
			return paySign;
		}
		public void setPaySign(String paySign) {
			this.paySign = paySign;
		}
	}
	
	public OrderResponse(GetOrderResponse res) {
		if (res != null && res.isSuccess()) {
			this.code = HttpServletResponse.SC_OK;
			this.payconfig = new PayConfig(res);
		} else {
			this.code = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
		}
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public PayConfig getPayconfig() {
		return payconfig;
	}

	public void setPayconfig(PayConfig payconfig) {
		this.payconfig = payconfig;
	}
	
}
